package com.huarui.util;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JSONUtils;

/**
 * 处理json的工具类，负责json数据转换成java对象和java对象转换成json
 * 
 * @author yongtree
 * @date 2008-11-22 上午10:47:13
 * @version 1.0
 */
public class JsonUtil {
	public static final String RESULT_TYPE_SUCCESS="success";
	public static final String RESULT_SUCCESS="OK";
	public static final String RESULT_FAILURE="failure";
	public static final String RESULT_MSG="msg";
	static {
		
	}
	/**
	 * 从一个JSON 对象字符格式中得到一个java对象
	 * 
	 * @param jsonString
	 * @param pojoCalss
	 * @return
	 */
	public static Object getObject4JsonString(String jsonString, Class pojoCalss) {
		Object pojo;
		JSONObject jsonObject = JSONObject.fromObject(jsonString);
		pojo = JSONObject.toBean(jsonObject, pojoCalss);
		return pojo;
	}

	/**
	 * 从json HASH表达式中获取一个map，改map支持嵌套功能
	 * 
	 * @param jsonString
	 * @return
	 */
	public static Map getMap4Json(String jsonString) {
		JSONObject jsonObject = JSONObject.fromObject(jsonString);
		Iterator keyIter = jsonObject.keys();
		String key;
		Object value;
		Map valueMap = new HashMap();

		while (keyIter.hasNext()) {
			key = (String) keyIter.next();
			value = jsonObject.get(key);
			valueMap.put(key, value);
		}

		return valueMap;
	}

	/**
	 * 从json数组中得到相应java数组
	 * 
	 * @param jsonString
	 * @return
	 */
	public static Object[] getObjectArray4Json(String jsonString) {
		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		return jsonArray.toArray();

	}

	/**
	 * 从json对象集合表达式中得到一个java对象列表
	 * 
	 * @param jsonString
	 * @param pojoClass
	 * @return
	 */
	public static List getList4Json(String jsonString, Class pojoClass) {

		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		JSONObject jsonObject;
		Object pojoValue;

		List list = new ArrayList();
		for (int i = 0; i < jsonArray.size(); i++) {

			jsonObject = jsonArray.getJSONObject(i);
			pojoValue = JSONObject.toBean(jsonObject, pojoClass);
			list.add(pojoValue);

		}
		return list;

	}

	/**
	 * 从json数组中解析出java字符串数组
	 * 
	 * @param jsonString
	 * @return
	 */
	public static String[] getStringArray4Json(String jsonString) {

		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		String[] stringArray = new String[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); i++) {
			stringArray[i] = jsonArray.getString(i);

		}

		return stringArray;
	}

	/**
	 * 从json数组中解析出javaLong型对象数组
	 * 
	 * @param jsonString
	 * @return
	 */
	public static Long[] getLongArray4Json(String jsonString) {

		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		Long[] longArray = new Long[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); i++) {
			longArray[i] = jsonArray.getLong(i);

		}
		return longArray;
	}

	/**
	 * 从json数组中解析出java Integer型对象数组
	 * 
	 * @param jsonString
	 * @return
	 */
	public static Integer[] getIntegerArray4Json(String jsonString) {

		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		Integer[] integerArray = new Integer[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); i++) {
			integerArray[i] = jsonArray.getInt(i);

		}
		return integerArray;
	}

	/**
	 * 从json数组中解析出java Integer型对象数组
	 * 
	 * @param jsonString
	 * @return
	 */
	public static Double[] getDoubleArray4Json(String jsonString) {

		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		Double[] doubleArray = new Double[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); i++) {
			doubleArray[i] = jsonArray.getDouble(i);

		}
		return doubleArray;
	}

	/**
	 * 将java对象转换成json字符串
	 * 
	 * @param javaObj
	 * @return
	 */
	public static String getJsonString4JavaPOJO(Object javaObj) {

		JSONObject json;
		json = JSONObject.fromObject(javaObj);
		return json.toString();

	}

	/**
	 * 将json字符串转换成jsonObject对象，将json字符串的空值替换成null
	 * 
	 * @param jsonString
	 * @return jsonObject
	 */
	public static JSONObject getJSONObjectFromString(String jsonString,Class rootClass) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(String.class,new JsonBlankValueProcessor());
		JSONObject jsonObject = JSONObject.fromObject(jsonString, jsonConfig);	
		return jsonObject;
	}

	public static JSONObject getJSONObjectFromBean(Object beanObject) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerDefaultValueProcessor(Integer.class,new JsonDefaultValueProcessor());		
		//jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONObject jsonObject = JSONObject.fromObject(beanObject, jsonConfig);
		return jsonObject;
	}
	public static JSONObject getJsonConfig(Object beanObject){
		JsonConfig jsonConfig=new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor());
		JSONObject jsonObject = JSONObject.fromObject(beanObject, jsonConfig);
		return jsonObject;
	}
	@SuppressWarnings("unchecked")
	public static <T> T getBean(Object beanObject,Class<T> targetClass){
		JSONObject jsonObject = JSONObject.fromObject(beanObject);
		setDataFormat2JAVA();
		T bean=(T) JSONObject.toBean(jsonObject, targetClass);
		return bean;
	}
	@SuppressWarnings("unchecked")
	public static <T> T getBean(JSONObject beanObject,Class<T> targetClass,String format){
		//JSONObject jsonObject = JSONObject.fromObject(beanObject);
		setDataFormat(format);
		T bean=(T) JSONObject.toBean(beanObject, targetClass);
		return bean;
	}
	@SuppressWarnings("unchecked")
	public static <T> List<T> getList(Object beanObject,Class<T> targetClass){
		JSONArray jsonArray=JSONArray.fromObject(beanObject);
		List<T> retList=new ArrayList<T>();
		for(Object obj:jsonArray.toArray()){
			JSONObject jsonObject=(JSONObject) obj;
			T bean=(T) JSONObject.toBean(jsonObject, targetClass);
			retList.add(bean);
		}
		return retList;
	}
	private static void setDataFormat2JAVA(){   
	 //设定日期转换格式   
		JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] {"yyyy-MM-dd HH:mm:ss"}));     
	}   
	
	private static void setDataFormat(String format){   
		 //设定日期转换格式   
			JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] {format}));     
		}   
}